<?php

class SearchController extends Controller{
    public function actionIndex($id='',$page='1',$perpage='10',$order='1',$feature='',$manufacturer='',$pricerange=''){

        $this->breadcrumbs=array(
      			'Поиск',
      		);

        echo CHtml::tag('title',array(),'Поиск | '.CHtml::encode(Yii::app()->name))."\n";

        if(isset($_POST['text']) AND $_POST['text'] != ''){
            $text = $_POST['text'];
            $categories = Category::model()->onlySecondLevel()->findAll("title like :text", array(':text'=>'%'.$text.'%'));
            $items = Item::model()->findAll("title like :text", array(':text'=>'%'.$text.'%'));
            $manufacturers = Manufacturer::model()->findAll("title like :text", array(':text'=>'%'.$text.'%'));
            $pages = Page::model()->findAll("title like :text", array(':text'=>'%'.$text.'%'));
            $contents = Content::model()->findAll("title like :text", array(':text'=>'%'.$text.'%'));
        }else{
            $text = '';
            $categories = false;
            $items = false;
            $manufacturers = false;
            $pages = false;
            $contents = false;
        }

        $this->yiistoRender('index',array(
            'categories'=>$categories,
            'items' => $items,
            'manufacturers' => $manufacturers,
            'pages' => $pages,
            'contents' => $contents,
            'text' => $text,
        ));

    }

    public function actionAjax(){

        if (isset($_GET['q'])) {

   			$criteria = new CDbCriteria;
   			$criteria->condition = 'title LIKE :query OR description LIKE :query';
   			$criteria->params = array(':query'=>'%'.$_GET['q'].'%');

   			$criteria->order = 'RAND()';

   			if (isset($_GET['limit']) && is_numeric($_GET['limit'])) {
   				$criteria->limit = $_GET['limit'];
   			}


            $resStr = '';
            $categories = Category::model()->onlySecondLevel()->findAll($criteria);
            foreach ($categories as $category) {
                $resStr .= $category->title."\n";
            }
            $items = Item::model()->findAll($criteria);
   			foreach ($items as $item) {
   				$resStr .= $item->title."\n";
   			}
   			echo $resStr;
   		}

    }
}